package com.ibm.csr.staf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TCLoader {

	private HashMap<String, String> tcmap = new HashMap<String, String>();
	private HashMap<String, Case> stc = new HashMap<String, Case>();// special
	private List<Case> tclist = new ArrayList<Case>();
	// testcases
	private String jccPrefix = "com.ibm.db2.jcc.harness.fvt.";

	public TCLoader() {
		initTCMap((TCLoader.class.getResource("/").getPath() + "bucklist.txt"));
		loadSTC((TCLoader.class.getResource("/").getPath() + "tclist.txt"));
	}

	public void generateTcList(String path) {
		File tcfile = new File(path);
		if (!tcfile.isFile()) {
			MSGLog.log(path + " is not exist!");
			return;
		}

		// Generate tclist
		try {
			BufferedReader br = new BufferedReader(new FileReader(tcfile));
			String line;
			String bucket;
			while ((line = br.readLine()) != null) {
				line = line.trim();
				if (line.equals("") || line.startsWith("#")) {
					continue;
				}
				bucket = line.substring(0, line.indexOf(".")) + ".bucket";
				if (stc.containsKey(line)) {// It's a special testcase.
					tclist.add(stc.get(line));
				} else if (stc.containsKey(bucket)) {// The parent's bucket is
					// special
					CSBucket s = (CSBucket) stc.get(bucket);
					CSTestCase n = new CSTestCase();
					n.setCaseName(generateClassName(line));
					n.setJavaVersion(s.getJavaVersion());
					n.setJccVersion(s.getJccVersion());
					n.setPrivateCommand(s.getPrivateCommand());
					n.setNewHarness(s.isNewHarness());
					tclist.add(n);
				} else {// It's a normal testcase;
					tclist.add(generateCase(line));
				}
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Start to kick off
	}

	public Case generateCase(String tcname) {
		String classname = generateClassName(tcname);
		if (!tcname.contains(".bucket")) {
			CSTestCase tc = new CSTestCase();
			tc.setCaseName(classname);
			return tc;
		} else {
			CSBucket tc = new CSBucket();
			tc.setCaseName(classname);
			return tc;
		}
	}

	public void loadSTC(String path) {
		File tcfile = new File(path);
		if (!tcfile.isFile()) {
			return;
		}
		try {
			FileReader fr = new FileReader(tcfile);
			BufferedReader br = new BufferedReader(fr);
			String line;

			while ((line = br.readLine()) != null) {
				if (line.trim().equals("") || line.startsWith("#")) {
					continue;
				}
				String[] pros = line.trim().split("\\|");
				if (pros.length != 5) {
					MSGLog.log("===== Analyze Error =====");
					MSGLog.log(line);
					continue;
				}
				String classname = generateClassName(pros[0].trim());
				if (classname == null) {
					MSGLog.log("===== Generate Class Name Error =====");
					MSGLog.log(line);
					continue;
				}
				if (!line.contains(".bucket")) {
					CSTestCase tc = new CSTestCase();
					tc.setCaseName(classname);
					tc.setJavaVersion(pros[1].trim());
					tc.setJccVersion(pros[2].trim());
					tc.setNewHarness(pros[3].trim().equals("new"));
					tc.setPrivateCommand(pros[4].trim());
					stc.put(pros[0].trim(), tc);
				} else {
					CSBucket tc = new CSBucket();
					tc.setCaseName(classname);
					tc.setJavaVersion(pros[1].trim());
					tc.setJccVersion(pros[2].trim());
					tc.setNewHarness(pros[3].trim().equals("new"));
					tc.setPrivateCommand(pros[4].trim());
					stc.put(pros[0].trim(), tc);
				}
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public List<Case> loadTC(String path) {
		List<Case> tclist = new ArrayList<Case>();
		File tcfile = new File(path);
		if (!tcfile.isFile()) {
			return tclist;
		}
		try {
			FileReader fr = new FileReader(tcfile);
			BufferedReader br = new BufferedReader(fr);
			String line;

			while ((line = br.readLine()) != null) {
				if (line.trim().equals("") || line.startsWith("#")) {
					continue;
				}
				String[] pros = line.trim().split("\\|");
				if (pros.length != 5) {
					MSGLog.log("===== Analyze Error =====");
					MSGLog.log(line);
					continue;
				}
				String classname = generateClassName(pros[0].trim());
				if (classname == null) {
					MSGLog.log("===== Generate Class Name Error =====");
					MSGLog.log(line);
					continue;
				}
				if (!line.contains(".bucket")) {
					CSTestCase tc = new CSTestCase();
					tc.setCaseName(classname);
					// tc.setTestcaseName(pros[0].trim());
					tc.setJavaVersion(pros[1].trim());
					tc.setJccVersion(pros[2].trim());
					tc.setNewHarness(pros[3].trim().equals("new"));
					tc.setPrivateCommand(pros[4].trim());
					tclist.add(tc);
				} else {
					CSBucket tc = new CSBucket();
					tc.setCaseName(classname);
					// tc.setTestcaseName(pros[0].trim());
					tc.setJavaVersion(pros[1].trim());
					tc.setJccVersion(pros[2].trim());
					tc.setNewHarness(pros[3].trim().equals("new"));
					tc.setPrivateCommand(pros[4].trim());
					tclist.add(tc);
				}

			}

			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return tclist;
	}

	public String generateClassName(String tcname) {
		if (!tcname.contains(".")) {
			MSGLog.log("No a correct TC name");
			return null;
		}

		String bucketclass = tcmap.get(tcname.substring(0, tcname
				.lastIndexOf(".")));
		// Can not find the bucket
		if (bucketclass == null) {
			MSGLog.log("The testcase name is not in the bucklist.txt");
			return null;
		}

		// If it's a bucket
		if (tcname.endsWith(".bucket")) {
			return jccPrefix + bucketclass;
		}

		// If it's a testcase
		return jccPrefix
				+ bucketclass.substring(0, bucketclass.lastIndexOf("."))
				+ tcname.substring(tcname.indexOf("."));
	}

	public void initTCMap(String path) {
		File bucklist = new File(path);
		if (!bucklist.isFile()) {
			return;
		}

		try {
			BufferedReader br = new BufferedReader(new FileReader(bucklist));
			String buck = "";
			while ((buck = br.readLine()) != null) {
				buck = buck.trim();
				if (buck.equals("") || buck.startsWith("#")) {
					continue;
				}
				String[] bucks = buck.split(",");
				if (bucks.length < 6) {
					MSGLog.log("Analyze bucklist Error");
					MSGLog.log(buck);
					continue;
				}
				tcmap.put(bucks[0].trim(), bucks[5].trim());
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		RunConfigure conf = new RunConfigure();
		// conf.setClientName("db2csnt1.svl.ibm.com");
//		 conf.setClientName("db2csnt7.svl.ibm.com");
		conf.setClientName("db2csnt0.svl.ibm.com");
//		 conf.setClientName("db2csw03.svl.ibm.com");

		conf.setServerName("csrec448.vmec.svl.ibm.com");
		// conf.setServerName("csrec056.vmec.svl.ibm.com");
		RunThread rt = new RunThread(conf);
		TCLoader loader = new TCLoader();
		loader.generateTcList("E:/workspace/csr-staf/src/kicklist.txt");
		// List tcs = loader.loadTC("E:/workspace2/csr-staf/src/tclist.txt");
		List<Case> tcs = loader.getTclist();
		try {
			rt.StartKickOff(tcs);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public List<Case> getTclist() {
		return tclist;
	}

	public void setTclist(List<Case> tclist) {
		this.tclist = tclist;
	}
}
